//
//  AppleScriptDevice.m
//  Shion
//
//  Created by Chris Karr on 12/19/07.
//  Copyright 2007-2008 Audacious Software. All rights reserved.
//

#import "AppleScriptDevice.h"
#import "ShionAppDelegate.h"

@implementation AppleScriptDevice

@synthesize device;

- (NSString *) name
{
	return device.name;
}

- (NSString *) address
{
	return [device addressString];
}

- (NSString *) id
{
	return [self address];
}

- (BOOL) dimmer
{
	return device.dimmer;
}

- (NSNumber *) level
{
	if (device.actualStatus.lightLevel != nil)
		return [NSNumber numberWithInteger:([device.actualStatus.lightLevel integerValue] + 1) / 32];

	return nil;
}

- (NSNumber *) absoluteLevel
{
	if (device.actualStatus.lightLevel != nil)
		return [NSNumber numberWithInteger:[device.actualStatus.lightLevel integerValue]];

	return nil;
}

- (NSString *) rawLevel
{
	if (device.actualStatus.lightLevel != nil)
	{
		if ([device.actualStatus.lightLevel integerValue] == 0)
			return @"off";
		else if ([device.actualStatus.lightLevel integerValue] == 255)
			return @"on";
		else
			return [NSString stringWithFormat:@"%d", [device.actualStatus.lightLevel integerValue]];
	}

	return @"unknown";
}

- (NSScriptObjectSpecifier *) objectSpecifier 
{
	NSArray * devices = [[NSApp delegate] valueForKey:@"devices"];
	
    unsigned index = [devices indexOfObjectIdenticalTo:self];
	
    if (index != NSNotFound) 
	{
		NSScriptObjectSpecifier * containerRef = [NSApp objectSpecifier];

        return [[[NSIndexSpecifier alloc] initWithContainerClassDescription:[containerRef keyClassDescription] 
			containerSpecifier:containerRef key:@"devices" index:index] autorelease];
    }
	else
        return nil;
}

-(void) activate:(NSScriptCommand *) command
{
	self.device.visibleStatus.lightLevel = [NSNumber numberWithInteger:255];
}

-(void) deactivate:(NSScriptCommand *) command
{
	self.device.visibleStatus.lightLevel = [NSNumber numberWithInteger:0];
}

-(void) dim:(NSScriptCommand *) command
{
	NSInteger todo = [self.device.visibleStatus.lightLevel integerValue] - 32;
		
	if (todo < 0)
		todo = 0;
		
	self.device.visibleStatus.lightLevel = [NSNumber numberWithInteger:todo];
}

-(void) brighten:(NSScriptCommand *) command
{
	NSInteger todo = [self.device.visibleStatus.lightLevel integerValue] + 32;
		
	if (todo > 255)
		todo = 255;
		
	self.device.visibleStatus.lightLevel = [NSNumber numberWithInteger:todo];
}

-(void) setLevel:(NSNumber *) newLevel
{
	NSInteger todo = ([newLevel integerValue] * 32) + 1;
	
	if (todo < 0)
		todo = 0;
	else if (todo > 255)
		todo = 255;
	
	self.device.visibleStatus.lightLevel = [NSNumber numberWithInteger:todo];
}

-(void) setAbsoluteLevel:(NSNumber *) newLevel
{
	NSInteger todo = [newLevel integerValue];
	
	if (todo < 0)
		todo = 0;
	else if (todo > 255)
		todo = 255;
	
	self.device.visibleStatus.lightLevel = [NSNumber numberWithInteger:todo];
}

- (void) removeDevice:(NSScriptCommand *) command
{
	NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
	[note setValue:self.device forKey:DEVICE];
	
	[[NSNotificationCenter defaultCenter] postNotificationName:REMOVE_DEVICE object:self userInfo:note];
}

@end
